package com.github.zxhtom.excel.expose;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.github.zxhtom.excel.Excel;
import com.github.zxhtom.excel.constant.HeadConstant;
import com.github.zxhtom.excel.model.First;
import com.github.zxhtom.excel.model.Second;
import com.github.zxhtom.excel.model.Student;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @package com.github.zxhtom.excel.expose
 * @Class EasyPoiTools
 * @Description TODO
 * @Author zhangxinhua
 * @Date 19-11-7 上午11:03
 */
public class EasyPoiTools<T> implements Excel<T> {

    @Override
    public void readData() {

    }

    @Override
    public void export(List<T> ts) {
        String parentFileName = EasyExcelTools.class.getResource("/").getPath() + "student/easypoi/";
        File file = new File(parentFileName);
        if (!file.exists()) {
            file.mkdirs();
        }
        String fileName = parentFileName + System.currentTimeMillis() + ".xlsx";
        List<Second> seconds = new ArrayList<>();
        Second second = new Second();
        second.setTs((List<Student>) ts);
        seconds.add(second);
        First first = new First();
        first.setSeconds(seconds);
        List<First> firsts = new ArrayList<>();
        firsts.add(first);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), First.class, firsts);
        workbook.setSheetName(0, HeadConstant.FIRSTNAME);
        workbook.getSheetAt(0).addMergedRegion(new CellRangeAddress(0,0,0,5));
        try {
            workbook.write(new FileOutputStream(new File(fileName)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
